package com.fd.servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;

public class DispatchServlet extends HttpServlet {
    private int count;
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        handleRequest(request,response);
    }

    private void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("Dispatc辅导费hServttlet.doGet");
        StringBuffer requestURL = request.getRequestURL();
        System.out.println("requestURL = " + requestURL);
        System.out.println("request.getRequestURI() = " + request.getRequestURI());
        System.out.println("request.getContextPath() = " + request.getContextPath());
        count++;
        System.out.println("request.getRemoteAddr() = " + request.getRemoteAddr());
        System.out.println("request.getRemoteHost() = " + request.getRemoteHost());
        System.out.println("count = " + count);
//        response.setStatus(300);
        String method = request.getMethod();
        //获取请求的url
        String url = request.getRequestURL()+"";
        //获取uri
        String uri = request.getRequestURI();
        //获取请求的参数  get
        String query = request.getQueryString();
        //获取请求人的id
        String ip = request.getRemoteAddr();
        //获取请求的主机名
        String host = request.getRemoteHost();

        HttpSession session = request.getSession();
//        从session得到当前用户的登录信息


        System.out.println(method+" url:"+url+" uri:"+
                uri+" query:"+query+" ip:"+ip+" host:"+host);

        if (request.getRequestURI().contains("session.do")) {
            HttpSession session1 = request.getSession();
//            让session失效,等价于用户的注销
            session1.invalidate();
        }

        if (request.getRequestURI().contains("addStudent.do")) {
//            用来获取前台传入的数据
            String studentName = request.getParameter("studentName");
            String[] likes = request.getParameterValues("likes");
            for (String like : likes) {
                System.out.println("like = " + like);
            }

            //从后台向前台传数据,使用setAttribute进行设置
            request.setAttribute("studentName","wangmazi");
            User user = new User(11, "lisi");

            //把要传到前端的对象放入到域对象中
            request.setAttribute("user",user);

            List list = Arrays.asList("lanqiu", "zuqiu", "eat", "dev");
            request.setAttribute("list",list);

            request.getRequestDispatcher("student.jsp").forward(request,response);
        }
        if (request.getRequestURI().contains("userDelete.do")) {
//            可以调用删除方法
        }
        String realPath = request.getServletContext().getRealPath("/");
        System.out.println("realPath = " + realPath);
        System.out.println("this = " + this);

        Enumeration<String> parameterNames = request.getParameterNames();
        while (parameterNames.hasMoreElements()) {
            String s = parameterNames.nextElement();
            System.out.println("s = " + s);
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        handleRequest(request, response);
    }
}
